{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:Load quantized model will cause accuracy drop.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:74: The name tf.gfile.GFile is deprecated. Please use tf.io.gfile.GFile instead.\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:74: The name tf.gfile.GFile is deprecated. Please use tf.io.gfile.GFile instead.\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:76: The name tf.GraphDef is deprecated. Please use tf.compat.v1.GraphDef instead.\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:76: The name tf.GraphDef is deprecated. Please use tf.compat.v1.GraphDef instead.\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:51: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:51: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:66: The name tf.InteractiveSession is deprecated. Please use tf.compat.v1.InteractiveSession instead.\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:66: The name tf.InteractiveSession is deprecated. Please use tf.compat.v1.InteractiveSession instead.\n",
      "\n",
      "WARNING:root:Load quantized model will cause accuracy drop.\n"
     ]
    }
   ],
   "source": [
    "import malaya\n",
    "\n",
    "model = malaya.dependency.transformer(model = 'xlnet', quantized = True)\n",
    "pos = malaya.pos.transformer(model = 'xlnet', quantized = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = 'Dia suka membaca buku.'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = malaya.preprocessing.TOKENIZER().tokenize\n",
    "sastrawi = malaya.stem.sastrawi()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "graph, tagging, indexing = model.predict(' '.join(tokenizer(t)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
       " -->\n",
       "<!-- Title: G Pages: 1 -->\n",
       "<svg width=\"244pt\" height=\"305pt\"\n",
       " viewBox=\"0.00 0.00 244.00 305.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 301)\">\n",
       "<title>G</title>\n",
       "<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-301 240,-301 240,4 -4,4\"/>\n",
       "<!-- 0 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>0</title>\n",
       "<text text-anchor=\"middle\" x=\"119\" y=\"-275.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">0 (None)</text>\n",
       "</g>\n",
       "<!-- 2 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>2</title>\n",
       "<text text-anchor=\"middle\" x=\"119\" y=\"-188.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">2 (suka)</text>\n",
       "</g>\n",
       "<!-- 0&#45;&gt;2 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>0&#45;&gt;2</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M119,-260.9735C119,-249.1918 119,-233.5607 119,-220.1581\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"122.5001,-220.0033 119,-210.0034 115.5001,-220.0034 122.5001,-220.0033\"/>\n",
       "<text text-anchor=\"middle\" x=\"130\" y=\"-231.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">root</text>\n",
       "</g>\n",
       "<!-- 1 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>1</title>\n",
       "<text text-anchor=\"middle\" x=\"28\" y=\"-101.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">1 (Dia)</text>\n",
       "</g>\n",
       "<!-- 2&#45;&gt;1 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>2&#45;&gt;1</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M100.1447,-173.9735C86.8452,-161.2586 68.8562,-144.0603 54.1685,-130.0183\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"56.4779,-127.384 46.8311,-123.0034 51.6406,-132.4437 56.4779,-127.384\"/>\n",
       "<text text-anchor=\"middle\" x=\"94\" y=\"-144.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">nsubj</text>\n",
       "</g>\n",
       "<!-- 3 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>3</title>\n",
       "<text text-anchor=\"middle\" x=\"119\" y=\"-101.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">3 (membaca)</text>\n",
       "</g>\n",
       "<!-- 2&#45;&gt;3 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>2&#45;&gt;3</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M119,-173.9735C119,-162.1918 119,-146.5607 119,-133.1581\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"122.5001,-133.0033 119,-123.0034 115.5001,-133.0034 122.5001,-133.0033\"/>\n",
       "<text text-anchor=\"middle\" x=\"138\" y=\"-144.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">xcomp</text>\n",
       "</g>\n",
       "<!-- 5 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>5</title>\n",
       "<text text-anchor=\"middle\" x=\"209\" y=\"-101.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">5 (.)</text>\n",
       "</g>\n",
       "<!-- 2&#45;&gt;5 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>2&#45;&gt;5</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M141.1903,-173.7481C147.7007,-168.1893 154.7479,-161.9681 161,-156 169.2647,-148.1108 177.9294,-139.1417 185.5869,-130.9485\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"188.3551,-133.11 192.5741,-123.3914 183.2154,-128.3578 188.3551,-133.11\"/>\n",
       "<text text-anchor=\"middle\" x=\"191.5\" y=\"-144.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">punct</text>\n",
       "</g>\n",
       "<!-- 4 -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>4</title>\n",
       "<text text-anchor=\"middle\" x=\"119\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">4 (buku)</text>\n",
       "</g>\n",
       "<!-- 3&#45;&gt;4 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>3&#45;&gt;4</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M119,-86.9735C119,-75.1918 119,-59.5607 119,-46.1581\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"122.5001,-46.0033 119,-36.0034 115.5001,-46.0034 122.5001,-46.0033\"/>\n",
       "<text text-anchor=\"middle\" x=\"128\" y=\"-57.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">obj</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x7f92204ea080>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graph.to_graphvis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Dia', 'PRON'),\n",
       " ('suka', 'VERB'),\n",
       " ('membaca', 'VERB'),\n",
       " ('buku', 'NOUN'),\n",
       " ('.', 'PUNCT')]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pos.predict(' '.join(tokenizer(t)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Ahmad', 'PROPN'),\n",
       " ('menendang', 'VERB'),\n",
       " ('bola', 'NOUN'),\n",
       " ('itu', 'DET'),\n",
       " ('.', 'PUNCT')]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.stack.voting_stack([pos] * 3, ' '.join(tokenizer('Ahmad menendang bola itu.')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Dia', 'PRON'),\n",
       " ('memberi', 'VERB'),\n",
       " ('saya', 'PRON'),\n",
       " ('sebatang', 'DET'),\n",
       " ('pena', 'NOUN'),\n",
       " ('.', 'PUNCT')]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.stack.voting_stack([pos] * 3, ' '.join(tokenizer('Dia memberi saya sebatang pena.')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Dia', 'PRON'),\n",
       " ('memberikan', 'VERB'),\n",
       " ('sebatang', 'NOUN'),\n",
       " ('pena', 'NOUN'),\n",
       " ('kepada', 'ADP'),\n",
       " ('saya', 'PRON'),\n",
       " ('.', 'PUNCT')]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.stack.voting_stack([pos] * 3, ' '.join(tokenizer('Dia memberikan sebatang pena kepada saya.')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = malaya.stack.voting_stack([pos] * 3, ' '.join(tokenizer(t)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "start_end = {'me': 'kan', 'mem': 'kan', 'men': 'kan', 'mem': '', 'me': '', 'men': ''}\n",
    "\n",
    "def check_transitif(word):\n",
    "    stemmed = sastrawi.stem(word)\n",
    "    for k, v in start_end.items():\n",
    "        if word.startswith(k) and word.endswith(v) and f'{k}{stemmed}{v}' == word:\n",
    "            return True\n",
    "    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import copy\n",
    "import re\n",
    "from malaya.text.regex import _expressions\n",
    "\n",
    "def reset_t(tokens):\n",
    "    t = []\n",
    "    for i in range(len(tokens)):\n",
    "        t.append([tokens[i], 2])\n",
    "    return t\n",
    "\n",
    "def augment_16_0(t, row):\n",
    "    text, tokens, tokens_lower, tagging = row\n",
    "    for i in range(len(tokens) - 1):\n",
    "        if tagging[i] in ['PRON', 'NOUN'] \\\n",
    "        and tagging[i + 1] == 'VERB' \\\n",
    "        and tagging[i + 2] in ['PRON', 'NOUN'] \\\n",
    "        and check_transitif(tokens[i + 1]):\n",
    "            t[i][1] = 16\n",
    "            t[i + 1][0] = sastrawi.stem(tokens[i + 1])\n",
    "            t[i + 1][1] = 16\n",
    "            t[i + 2][1] = 16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 4/4 [00:00<00:00,  5.00it/s]\n"
     ]
    }
   ],
   "source": [
    "results = []\n",
    "for text in tqdm(['Cerpen itu telah saya karang.', \n",
    "                  'Latihan itu mesti kau buat.',\n",
    "                  'Kereta itu saya beli daripada Ali.',\n",
    "                  'Dia memberi saya sebatang pena.']):\n",
    "    tokens = tokenizer(text)\n",
    "    t = reset_t(tokens)\n",
    "    t_ = copy.deepcopy(t)\n",
    "    tokens_lower = tokenizer(text.lower())\n",
    "    tagging = malaya.stack.voting_stack([pos] * 3, ' '.join(tokens))\n",
    "    tagging = list(zip(*tagging))[1]\n",
    "    r = (t, tokens, tokens_lower, tagging)\n",
    "    augment_16_0(t_, r)\n",
    "    results.append((t, t_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[([['Cerpen', 2],\n",
       "   ['itu', 2],\n",
       "   ['telah', 2],\n",
       "   ['saya', 2],\n",
       "   ['karang', 2],\n",
       "   ['.', 2]],\n",
       "  [['Cerpen', 2],\n",
       "   ['itu', 2],\n",
       "   ['telah', 2],\n",
       "   ['saya', 2],\n",
       "   ['karang', 2],\n",
       "   ['.', 2]]),\n",
       " ([['Latihan', 2],\n",
       "   ['itu', 2],\n",
       "   ['mesti', 2],\n",
       "   ['kau', 2],\n",
       "   ['buat', 2],\n",
       "   ['.', 2]],\n",
       "  [['Latihan', 2],\n",
       "   ['itu', 2],\n",
       "   ['mesti', 2],\n",
       "   ['kau', 2],\n",
       "   ['buat', 2],\n",
       "   ['.', 2]]),\n",
       " ([['Kereta', 2],\n",
       "   ['itu', 2],\n",
       "   ['saya', 2],\n",
       "   ['beli', 2],\n",
       "   ['daripada', 2],\n",
       "   ['Ali', 2],\n",
       "   ['.', 2]],\n",
       "  [['Kereta', 2],\n",
       "   ['itu', 2],\n",
       "   ['saya', 2],\n",
       "   ['beli', 2],\n",
       "   ['daripada', 2],\n",
       "   ['Ali', 2],\n",
       "   ['.', 2]]),\n",
       " ([['Dia', 2],\n",
       "   ['memberi', 2],\n",
       "   ['saya', 2],\n",
       "   ['sebatang', 2],\n",
       "   ['pena', 2],\n",
       "   ['.', 2]],\n",
       "  [['Dia', 16],\n",
       "   ['beri', 16],\n",
       "   ['saya', 16],\n",
       "   ['sebatang', 2],\n",
       "   ['pena', 2],\n",
       "   ['.', 2]])]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
